package util;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySQLConnection {
    //修改IP和数据库名称,常量都是大写。
    private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=true&serverTimezone=GMT&characterEncoding=utf-8"; // 数据库地址
    private static final String USER = "root"; // 数据库用户名
    private static final String PASSWORD = "2992003lyabc123"; // 数据库密码
    public static Connection getConnection() {
        Connection connection = null;
        try {
            // 加载 JDBC 驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 获取数据库连接
            connection = DriverManager.getConnection(URL, USER, PASSWORD);
            System.out.println("数据库连接成功！");
        } catch (ClassNotFoundException e) {
            System.out.println("找不到 JDBC 驱动！" + e.getMessage());
        } catch (SQLException e) {
            System.out.println("数据库连接失败！" + e.getMessage());
        }
        return connection;
    }
}
